<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/28
 * Time: 13:38
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Information extends Base
{
    /**
     * 首页联盟资讯展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            // 标题
            if(isset($params['information_title']) && $params['information_title'] != ''){
                $where['information_title'] = ['like', '%' . $params['information_title'] . '%'];
            }
            // 分类
            if(isset($params['information_type']) && $params['information_type'] != ''){
                $where['information_type'] = $params['information_type'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_information')
                ->join('up_sheng', 'sheng_id = information_sheng_id', 'left')
                ->join('up_information_type', 'information_type = it_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_information')
                ->join('up_sheng', 'sheng_id = information_sheng_id', 'left')
                ->join('up_information_type', 'information_type = it_id', 'left')
                ->field('information_id, information_title, information_desc, information_label, 
                information_image, it_name, information_num, information_comment_num, information_time, 
                sheng_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('information_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加联盟资讯
     * @return mixed|\think\response\Json
     */
    public function informationAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['information_content'])){
                return json(['code' => 100, 'msg' => '请填写详情']);
            }
            $insert = [
                'information_title' => $params['information_title'],
                'information_content' => $params['information_content'],
                'information_desc' => $params['information_desc'],
                'information_label' => $params['information_label'],
                'information_image' => $params['information_image'],
                'information_sheng_id' => $params['information_sheng_id'],
                'information_type' => $params['information_type'],
                'information_place' => $params['information_place'],
                'information_time' => date('Y-m-d H:i:s'),
                'information_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_information')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 地区
        $sheng = Db::table('up_sheng')->field('sheng_id, sheng_name')->select();
        // 分类
        $type = Db::table('up_information_type')->field('it_id, it_name')->select();
        $this->assign([
            'sheng' => $sheng,
            'type' => $type
        ]);
        return $this->fetch('information/informationadd');
    }
    
    /**
     * 编辑联盟资讯
     * @return mixed|\think\response\Json
     */
    public function informationEdit(){
        $information_id = input('information_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['information_content'])){
                return json(['code' => 100, 'msg' => '请填写详情']);
            }
            $update = [
                'information_title' => $params['information_title'],
                'information_content' => $params['information_content'],
                'information_desc' => $params['information_desc'],
                'information_label' => $params['information_label'],
                'information_sheng_id' => $params['information_sheng_id'],
                'information_type' => $params['information_type'],
                'information_place' => $params['information_place'],
                'information_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($params['information_image'])){
                $update['information_image'] = $params['information_image'];
            }
            $res = Db::table('up_information')->where('information_id', $information_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $information = Db::table('up_information')
            ->field('information_title, information_content, information_desc, information_label, 
            information_image, information_sheng_id, information_type, information_place')
            ->where('information_id', $information_id)
            ->find();
        // 地区
        $sheng = Db::table('up_sheng')->field('sheng_id, sheng_name')->select();
        // 分类
        $type = Db::table('up_information_type')->field('it_id, it_name')->select();
        $this->assign([
            'information_id' => $information_id,
            'information' => $information,
            'sheng' => $sheng,
            'type' => $type
        ]);
        return $this->fetch('information/informationedit');
    }
    
    /**
     * 联盟资讯详情
     * @return mixed
     */
    public function informationDetail(){
        $information_id = input('information_id');
        $information = Db::table('up_information')
            ->field('information_title, information_content, information_desc, information_label, 
            information_image, information_sheng_id, information_type, information_place')
            ->where('information_id', $information_id)
            ->find();
        // 地区
        $sheng = Db::table('up_sheng')->field('sheng_id, sheng_name')->select();
        // 分类
        $type = Db::table('up_information_type')->field('it_id, it_name')->select();
        $this->assign([
            'information' => $information,
            'sheng' => $sheng,
            'type' => $type
        ]);
        return $this->fetch('information/informationdetail');
    }
    
    /**
     * 删除联盟资讯
     * @return \think\response\Json
     */
    public function informationDel(){
        $information_id = input('id');
        $res = Db::table('up_information')->where('information_id', $information_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}